package work;

public class test7{
    private static int j = 0;
    private static final Object lock = new Object();

    public static void main(String[] args) {
        Thread inThread1 = new Thread(() -> {
                synchronized (lock) {
                    j++;
                    System.out.println("加一线程 1: j = " + j);
            }
        });

        Thread inThread2 = new Thread(() -> {
                synchronized (lock) {
                    j++;
                    System.out.println("加一线程 2: j = " + j);
            }
        });

        Thread deThread1 = new Thread(() -> {
                synchronized (lock) {
                    j--;
                    System.out.println("减一线程 1: j = " + j);
            }
        });

        Thread deThread2 = new Thread(() -> {
                synchronized (lock) {
                    j--;
                    System.out.println("减一线程 2: j = " + j);
            }
        });

        inThread1.start();
        inThread2.start();
        deThread1.start();
        deThread2.start();
    }
}
